package matrix;

public class Utils {
	public static final int TYPE_FLOAT = 0, TYPE_DOUBLE = 1, TYPE_INTEGER = 2;
	
	private static Number parse(String s, int type) {
		if (s == null)
			s = "0";
		switch (type) {
		case Utils.TYPE_FLOAT:
			return Float.parseFloat(s);
		case Utils.TYPE_DOUBLE:
			return Double.parseDouble(s);
		case Utils.TYPE_INTEGER:
			return Integer.parseInt(s);
		}
		return 0;
	}
	
	public static Number convert(Number n, int type) {
		switch (type) {
		case Utils.TYPE_FLOAT:
			return n.floatValue();
		case Utils.TYPE_DOUBLE:
			return n.doubleValue();
		case Utils.TYPE_INTEGER:
			return n.intValue();
		}
		return 0;
	}
	
	public static int get(String s) {
		if (s.compareToIgnoreCase("integer") == 0)
			return TYPE_INTEGER;
		else if (s.compareToIgnoreCase("float") == 0)
			return TYPE_FLOAT;
		else if (s.compareToIgnoreCase("double") == 0)
			return TYPE_DOUBLE;
		else
			return TYPE_DOUBLE;
	}
	
	public static double compare(Number n1, Number n2) {
		return n1.doubleValue() - n2.doubleValue();
	}
	
	public static Number abs(Number n, int type) {
		return convert(Math.abs(n.doubleValue()), type);
	}
	
	public static void fill(Number[][] matrix, String[][] init, int type) {
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				matrix[i][j] = parse(init[i][j], type);
			}
		}
	}
}
